package cz.cernilovsky.android.profileapp.model;

import java.io.Serializable;

import com.google.android.gms.location.Geofence;

public class Location implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -7695860764959993755L;
	private int id;
	private String name;
	private Profile[] onEntryProfiles;
	private Profile[] onLeaveProfiles;
	private double latitude;
	private double longitude;
	private int radius;
	public static Geofence toGeofence(Location location, Geofence.Builder builder) {
		return builder.setRequestId(String.valueOf(location.getId())).setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT).setCircularRegion(location.getLatitude(), location.getLongitude(), location.getRadius()).setExpirationDuration(Geofence.NEVER_EXPIRE).build();
	}
	public Location(int id, String name, Profile[] onEntryProfiles,
			Profile[] onLeaveProfiles, double latitude, double longitude,
			int radius) {
		this(name, onEntryProfiles, onLeaveProfiles, latitude, longitude, radius);
		this.id = id;
	}
	public Location(String name, Profile[] onEntryProfiles,
			Profile[] onLeaveProfiles, double latitude, double longitude,
			int radius) {
		this.name = name;
		this.onEntryProfiles = onEntryProfiles;
		this.onLeaveProfiles = onLeaveProfiles;
		this.latitude = latitude;
		this.longitude = longitude;
		this.radius = radius;
	}
	public Location(String name, Object[] onEntryProfiles, Object[] onLeaveProfiles,
			double latitude, double longitude, int radius) {
		this(name, null, null, latitude, longitude, radius);
		Profile[] onEntryProfilesP = convertObjectToProfile(onEntryProfiles);
		this.onEntryProfiles = onEntryProfilesP;
		Profile[] onLeaveProfilesP = convertObjectToProfile(onLeaveProfiles);
		this.onLeaveProfiles = onLeaveProfilesP;
	}
	
	public Profile[] convertObjectToProfile(Object[] profiles) {
		Profile[] profilesP = new Profile[profiles.length];
		for(int i = 0; i < profiles.length; i++) {
			profilesP[i] = (Profile) profiles[i];
		}
		return profilesP;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Profile[] getOnEntryProfiles() {
		return onEntryProfiles;
	}
	public void setOnEntryProfiles(Profile[] onEntryProfiles) {
		this.onEntryProfiles = onEntryProfiles;
	}
	public Profile[] getOnLeaveProfiles() {
		return onLeaveProfiles;
	}
	public void setOnLeaveProfiles(Profile[] onLeaveProfiles) {
		this.onLeaveProfiles = onLeaveProfiles;
	}
	public double getLatitude() {
		return latitude;
	}
	public void setLatitude(double latitude) {
		this.latitude = latitude;
	}
	public double getLongitude() {
		return longitude;
	}
	public void setLongitude(double longitude) {
		this.longitude = longitude;
	}
	public int getRadius() {
		return radius;
	}
	public void setRadius(int radius) {
		this.radius = radius;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	@Override
	public String toString() {
		return getName().toString();
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Location other = (Location) obj;
		if (id != other.id)
			return false;
		return true;
	}
	public void setOnEntryProfiles(Object[] array) {
		onEntryProfiles = convertObjectToProfile(array);
	}
	public void setOnLeaveProfiles(Object[] array) {
		onLeaveProfiles = convertObjectToProfile(array);
	}
	
	
}
